﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

using UCore;
using System.Windows.Documents;
using System.Xml.Linq;

namespace Twin {
    public partial class frmRdBelt : Form {


        public string GeneratedString { get; private set; }
        private string[] arg;
        private string args;

        private int INST_BITS = 0;
        private int INST_OP = 1;
        private int INST_CMFG = 2;
        private int INST_IFMT = 3;

        //string absROOT;
        private XPort xport = new XPort();


        public frmRdBelt(XPort xport, string content) {      //OP_RD_BELT, [cmfg], [i_fmt]
            InitializeComponent();

            GeneratedString = content;
            this.xport = xport;

            //dgvEdit
            dgvGI.ColumnHeadersVisible = false;
            dgvGI.AllowUserToAddRows = false;
            // 为DataGridView的MouseClick事件添加事件处理器
            dgvGI.MouseClick += dgvGI_MouseClick;


            arg = content.Split(',');

            arg[INST_CMFG] = UI.init_cmbCmfg(cmbCmfg, arg[INST_CMFG]);
            //UI.init_cmbCom(cmbCom, Ini.defaultPort);

            string s = Common.RemoveBracketedContent(arg[INST_IFMT]);
            if (s.Equals("")) return;
            arg[INST_IFMT] = s;
            int iFmt = Convert.ToInt32(s, 16);

            UI.init_cmbIFmt(cmbIFmt, iFmt);
            UI.init_listEls(listEls);
            UI.init_dgvGI(dgvGI, iFmt);

            //inst[INST_AREA_SN] = (Twin.TConsole.SnFile++).ToString("x");
        }


        private void frmRdBelt_Load(object sender, EventArgs e) {
            this.TopMost = true;
            this.FormBorderStyle = FormBorderStyle.FixedSingle; // 设置窗体边框样式为固定单线框
            this.MaximizeBox = false;   // 移除最大化按钮
            this.MinimizeBox = false;   // 禁止调整大小

            TConsole.PositionFormAtTopRight(this);
        }


        //删除
        private void dgvGI_MouseClick(object sender, MouseEventArgs e) {
            // 检查是否是鼠标右键点击
            if (e.Button == MouseButtons.Right) {
                // 检查是否有当前行
                if (dgvGI.CurrentRow != null) {
                    // 显示确认消息框
                    DialogResult result = MessageBox.Show("确定要删除当前行吗？", "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    // 如果用户点击了“是”，则删除当前行
                    if (result == DialogResult.Yes) {
                        dgvGI.Rows.Remove(dgvGI.CurrentRow);
                    }
                    // 删除当前行
                    //dgvEdit.Rows.Remove(dgvEdit.CurrentRow);
                }
            }
        }


        //选择
        private void listEls_SelectedIndexChanged(object sender, EventArgs e) {
            // 检查是否有选中的项
            if (listEls.SelectedItem != null) {
                // 获取ListBox1中选中的项
                string selectedText = listEls.SelectedItem.ToString();
                // 将选中的项添加到DataGridView的尾部
                int newRow = dgvGI.Rows.Add(selectedText);
                // 设置新行的第一列为选中状态
                dgvGI.CurrentCell = dgvGI.Rows[newRow].Cells[0];
            }
        }


        //生成
        private void btnGenerate_Click_1(object sender, EventArgs e) {
            // 遍历DataGridView的所有行
            string s = "";
            foreach (DataGridViewRow row in dgvGI.Rows) {
                // 检查行是否为新行（即用户可以添加但未提交的行）
                if (!row.IsNewRow) {
                    // 访问第一列的值
                    s += row.Cells[1].Value.ToString();
                    s += ">";
                    //Console.WriteLine(value); // 打印到控制台，或者可以进行其他操作
                }
            }
            //if (!s.Equals("")) {
            //    inst[INST_KVS] = s.Substring(0, s.Length - 1);
            //}
            GeneratedString = arg[INST_BITS] + "," + arg[INST_OP] + "," + arg[INST_CMFG] + "[cmfg]," + arg[INST_IFMT] + "[i_fmt]";
            this.Close();

        }

        private void dgvGI_CellContentClick(object sender, DataGridViewCellEventArgs e) {        
        }


        private void cmbCmfg_SelectedIndexChanged(object sender, EventArgs e) {
            arg[INST_CMFG] = cmbCmfg.SelectedItem.ToString();
            if (arg[INST_CMFG].Equals("空")) {
                arg[INST_CMFG] = "";
            }
        }


        private void cmbIFmt_SelectedIndexChanged(object sender, EventArgs e) {
            arg[INST_IFMT] = cmbIFmt.SelectedItem.ToString();
            UI.init_dgvGI(dgvGI, Convert.ToInt32(arg[INST_IFMT]));
        }


        private void btnSend_Click(object sender, EventArgs e) {
            CInfo cInfo = new CInfo();

            Msg.setInfo_bits(ref cInfo, arg[INST_BITS]);

            arg[INST_IFMT] = cmbIFmt.Text;   //jointCxs(dgvInstant);
            cInfo.sOpArgs = arg[INST_OP] + "," + arg[INST_CMFG] + "," + arg[INST_IFMT];
            
            string wr_kvs = "";

            TConsole.sendAsProtocol(xport, ref cInfo, wr_kvs, "cobra");
        }


        private void label4_Click(object sender, EventArgs e) {

        }


        private void cmbCom_SelectedIndexChanged(object sender, EventArgs e) {

        }


        private void label2_Click(object sender, EventArgs e) {

        }
    }

}

